package com.epmis.epmis.controller;

import com.epmis.epmis.Service.LoginService;
import com.epmis.epmis.Service.UserService;
import com.epmis.epmis.pojo.User;
import com.epmis.epmis.utils.ImageUitl;
import com.epmis.epmis.utils.TokenUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Resource
    LoginService loginService;

    @Resource
    UserService userService;


    @PostMapping("/login")
    @ResponseBody
    public int Login(@RequestBody HashMap<String,Object> map){
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        return loginService.Login(username,password);
    }

    @PostMapping("/token")
    @ResponseBody
    public String Token(@RequestBody HashMap<String,Object> map) throws JsonProcessingException {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        String token= TokenUtil.sign(user);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("token",token);
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @PostMapping("/getUserMessage")
    @ResponseBody
    public User getUserMessage(@RequestBody HashMap<String,Object> map) throws IOException {
        String username = (String) map.get("username");
        User user =  loginService.getUserMessage(username);
        user.setStatus(userService.getPostName(user.getPost_id()).getPost_level());
        user.setPhoto("data:image/jpeg;base64,"+ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
        return user;
    }

    @PostMapping("/register")
    @ResponseBody
    public int Register(@RequestBody HashMap<String,Object> map){
        String enterpriseName = (String) map.get("enterprise");
        User user = new User();
        user.setName((String) map.get("name"));
        user.setPhone((String) map.get("phone"));
        user.setEmail((String) map.get("email"));
        user.setPassword((String) map.get("password"));
        user.setUsername((String) map.get("phone"));
        user.setPhoto("cat.jpg");
        return loginService.Register(user,enterpriseName);
    }

    @PostMapping("api/updatePassword")
    @ResponseBody
    public int updatePassword(@RequestBody HashMap<String,Object> map) {
        String password = (String) map.get("newPassword");
        String phone = (String) map.get("phone");
        return loginService.updatePassword(password,phone);
    }

    @PostMapping("api/EmailUpdatePassword")
    @ResponseBody
    public int EmailUpdatePassword (@RequestBody HashMap<String,Object> map) {
        String password = (String) map.get("newPassword1");
        String email = (String) map.get("email");
        long createTime = (long) map.get("createTime");
        long spent = new Date().getTime() - createTime;
        if (spent / (1000 * 60) > 5) {
            return 2;
        }else{
            return loginService.updatePasswordE(password,email);
        }

    }
}
